package com.roads.supervisor.controller;

import com.roads.common.annotation.Log;
import com.roads.common.constant.UserConstants;
import com.roads.common.core.controller.BaseController;
import com.roads.common.core.domain.AjaxResult;
import com.roads.common.core.page.TableDataInfo;
import com.roads.common.enums.BusinessType;
import com.roads.supervisor.domain.SupBoqTemplate;
import com.roads.supervisor.service.ISupBoqTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 监理清单模板Controller
 * 
 * @author nhp
 * @date 2022-07-09
 */
@RestController
@RequestMapping("/supervisor/template")
public class SupBoqTemplateController extends BaseController {

    @Autowired
    private ISupBoqTemplateService service;

    @Autowired
    private ISupBoqTemplateService boqTemplateService;

    /**
     * 查询列表
     */
    @PreAuthorize("@ss.hasPermi('supervisor:template:list')")
    @GetMapping("/list")
    public TableDataInfo list(SupBoqTemplate model) {
        startPage();
        List<SupBoqTemplate> list = service.selectModelList(model);
        return getDataTable(list);
    }

    /**
     * 获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('supervisor:template:query')")
    @GetMapping(value = "/{templateId}")
    public AjaxResult getInfo(@PathVariable("templateId") Integer templateId) {
        return AjaxResult.success(service.getModelById(templateId));
    }

    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('supervisor:template:add')")
    @Log(title = "监理清单模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SupBoqTemplate model) {
        // 检查模板名称是否存在
        if (UserConstants.NOT_UNIQUE.equals(service.checkNameUnique(model))) {
            return AjaxResult.error("新增模板'" + model.getTemplateName() + "'失败，模板名称已存在");
        }
        model.setCreateBy(getUsername());
        return toAjax(service.insert(model));
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('supervisor:template:edit')")
    @Log(title = "监理清单模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SupBoqTemplate model) {
        // 检查模板名称是否存在
        if (UserConstants.NOT_UNIQUE.equals(service.checkNameUnique(model))) {
            return AjaxResult.error("修改模板'" + model.getTemplateName() + "'失败，模板名称已存在");
        }
        model.setUpdateBy(getUsername());
        return toAjax(service.update(model));
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('supervisor:template:remove')")
    @Log(title = "监理清单模板", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        ///// 删除限制，查看该模板下是否有清单
        return toAjax(service.deleteByIds(ids));
    }

    /**
     * 清单模板下拉框查询
     *
     * @param institutionCode 标段编号
     * @return 结果
     */
    @GetMapping("/dropdownList")
    public AjaxResult dropdownList(String institutionCode) {
        SupBoqTemplate search = new SupBoqTemplate();
        search.setInstitutionCode(institutionCode);
        List<SupBoqTemplate> list = boqTemplateService.selectModelList(search);
        return AjaxResult.success(list);
    }
}
